
/*
 *	百度
 *	分享
 *	入口
*/

window._bd_share_config = {
//此处添加分享具体设置
	common : {		
		bdText : '央票通-全国首家专业为央企商票贴现融资的供应链互联网金融平台 - 央票通',	
		bdDesc : '',	
		bdUrl : 'http://www.yangpiaotong.com/', 	
		bdPic : 'http://www.yangpiaotong.com/public/attachment/197001/01/16/5678caba15375.png'
	},
	share : [{
		"bdSize" : 24
	}]
}

//以下为js加载部分
with(document)0[(getElementsByTagName('head')[0]||body)
.appendChild(createElement('script'))
.src='http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];


/**
 *  返回頂部
*/

function ifHide(){
    var h = 500;
    var t = $(document).scrollTop();
    if(t > h){
        $('#backTop').show();
    }else{
        $('#backTop').hide();
    }
}

$(document).ready(function(e) {

    ifHide();

    $('#backTop').click(function(){
        if(!$('html,body').is(':animated')){
            $('html,body').animate({
                scrollTop: '0px'
            }, 100);
        };	
    });

});

$(window).scroll(function(e){
    ifHide();
});

/**
 * 收益计算
*/

var $pCounter = $('#counterProfit');
var $mon = $("input[name='amountValue']");
var $day = $(".duration");
var $rate = $("input[name='annualRate']");
var $select = $('select[name="paymentMethod"]');

//判断是否计算
function pCount(){

    var removeNc = $("input.form-control").removeClass('nc');

    if ($mon.val() == "") {
        $(".errTip").html('请输入投资金额');
        removeNc;
        $mon.addClass('nc');
    }else if(!/^-?\d+\.?\d{0,2}$/.test($mon.val()) || $mon.val() < 0){
        $(".errTip").html('金额必须为正数,可保留两位小数');
        removeNc;
        $mon.addClass('nc');
    }else if ($day.val() == "") {
        $(".errTip").html('输入投资期限');
        removeNc;
        $day.addClass('nc');
    }else if(!/^\d+$/.test($day.val()) || $day.val() < 0){
        $(".errTip").html('投资期限必须为正整数！');
        removeNc;
        $day.addClass('nc');
    }else if ($rate.val() == "") {
        $(".errTip").html('输入年化利率');
        removeNc;
        $rate.addClass('nc');
    }else if(!/^-?\d+\.?\d{0,2}$/.test($rate.val()) || $rate.val() < 0){
        $(".errTip").html('利率必须为正数,可保留两位小数');
        removeNc;
        $rate.addClass('nc');
    }else{
        $(".errTip").html('');
        removeNc;
        $(".counter-result-container").css('width','568px');
        fruitCount();
    };

}

//计算方式
function fruitCount(){   

    var mydate = new Date();
    var dateY = mydate.getFullYear() + "年";
    var dateM = (mydate.getMonth()+1) + "月";
    var dateD = mydate.getDate() + 3 + "日";
    var time = dateY + dateM + dateD;

    $('.hint').html(time);

    if ($select.prop('selectedIndex') == 0) {

        selectX(time,parseFloat($mon.val()),parseInt($day.val()),parseFloat($rate.val()));

    }else if ($select.prop('selectedIndex') == 1) {

        selectY(time,parseFloat($mon.val()),parseInt($day.val()),parseFloat($rate.val()));

    }else{

        selectD(time,parseFloat($mon.val()),parseInt($day.val()),parseFloat($rate.val()));

    };

}

//计算结果
function addCount(year,month,day,amount,prin,interest,surplus,num){

    var str = "";    

    if (num > 1) {
        for (var i = 0; i < num; i++) {
            str += "<div class=\"tdContent\"> " ;
            str += "<div class=\"tdCell borderRight\">" ;
            if(month + i + 1 > 12){
                str += year + parseInt((month + i)/12) + "年" ;
                str += [month + i + 1 - parseInt((month + i)/12)*12];
            }else{
                str += year + "年" ;
                str += [month + i + 1];
            };
            str += "月" + day + "日" ;
            str += "</div>" ;        
            str += "<div class=\"tdCell borderRight\">" ; 
            if ( i < num - 1 ) {
                str += parseFloat(amount);  
            }else{
                str += parseFloat(amount) + surplus ;  
            };
            str += "</div>" ;      
            str += "<div class=\"tdCell borderRight\">" ;
            if ( i < num - 1 ) {
                str += prin;  
            }else{
                str += prin + surplus ;
            };
            str += "</div>" ;     
            str += "<div class=\"tdCell borderRight\">" ;
            str += interest ;
            str += "</div>" ;       
            str += "<div class=\"tdCell borderRight\">" ;
            if ( i < num - 1 ) {
                str += surplus;  
            }else{
                str += surplus - surplus ;
            };
            str += "</div>" ; 
            str += "</div>";
        };
    }else{
        str += "<div class=\"tdContent\"> " ;
        str += "<div class=\"tdCell borderRight\">" ;
        str += year + "年" + month + "月" + day + "日" ;
        str += "</div>" ;        
        str += "<div class=\"tdCell borderRight\">" ; 
        str += amount ;
        str += "</div>" ;      
        str += "<div class=\"tdCell borderRight\">" ;
        str += prin ;
        str += "</div>" ;     
        str += "<div class=\"tdCell borderRight\">" ;
        str += interest ;
        str += "</div>" ;       
        str += "<div class=\"tdCell borderRight\">" ;
        str += surplus ;
        str += "</div>" ; 
        str += "</div>";
    };

    var TamountPrincipal = surplus + prin;
    var TamountInterest = parseFloat((interest*num).toFixed(2));
    var Tamount = TamountPrincipal + TamountInterest;

    if($('.tdContent'))$('.tdContent').remove();
    $('#cc-cal-list-wp').append(str);
    $('.Tamount').html(Tamount);
    $('.TamountPrincipal').html(TamountPrincipal);
    $('.TamountInterest').html(TamountInterest);

}

//等额本息
function selectD(time,mon,days,rate){

    var year = parseInt(time.split('年')[0]);
    var month = parseInt(time.split('月')[0].split('年')[1]);
    var day = parseInt(time.split('日')[0].split('月')[1]);
    var num = days;

    addCount(year,month,day,amount.toFixed(2),prin,interest.toFixed(2),surplus,num)

}

//先息后本
function selectX(time,mon,days,rate){

    var year = parseInt(time.split('年')[0]);
    var month = parseInt(time.split('月')[0].split('年')[1]);
    var day = parseInt(time.split('日')[0].split('月')[1]);
    var num = days;
    var amount = mon * rate / 1200;
    var prin = 0;
    var interest = mon * rate / 1200;
    var surplus = mon;

    addCount(year,month,day,amount.toFixed(2),prin,interest.toFixed(2),surplus,num)
   
}

//一次性还本付息
function selectY(time,mon,days,rate){

    //年
    var year = parseInt(time.split('年')[0]);
    var month = parseInt(time.split('月')[0].split('年')[1]);
    var day = parseInt(time.split('日')[0].split('月')[1]);
    var surplus = 0;
    var num = 1;

    if($day.attr('name') == 'dueMonth'){
        var prin = mon;
        var interest = mon * rate * days / 1200;
        var amount = prin + interest;
        if (month + days > 12) {
            year += parseInt((month + days - 1) / 12);
            month += days - parseInt((month + days - 1) / 12) * 12;
        }else{
            month += days; 
        };
    }else if($day.attr('name') == 'dueDay'){
        var prin = mon;
        var interest = mon * rate * days / 1200 / 30;
        var amount = prin + interest;
        if(day + days > 30){
            month += parseInt((day + days - 1) / 30); 
            day += days - parseInt((day + days - 1) / 30) * 30;   
            if (month > 12) {
                year += parseInt(month/12);
                month -= parseInt(month/12)*12;
            };
        }else{
            day += days;
        };
    }

    addCount(year,month,day,amount.toFixed(2),prin,interest.toFixed(2),surplus,num);

}

//点击计算器
$pCounter.click(function(){

    $(".counter-box").show();

});

//关闭计算器
$(".btnClose").click(function(){

    $(".counter-box").hide();
    $(".counter-result-container").css('width','0');

});

//计算器计算
$(".btn-cal").click(function(){

    pCount();

});

//计算结果收起

$(".btnShow").click(function(){

    if ($(this).hasClass('iconleft') == false) {
        $(this).addClass('iconleft');
        $(".counter-result-container").css('width','46px');
    }else{
        $(this).removeClass('iconleft');
        $(".counter-result-container").css('width','568px');
    };
    
});

//计算天和月切换
$("input[name='dueUnit']").click(function(){

    if ($("input[name='dueUnit']:checked").attr('value') == "天") {
        $('span.changeUnit').html('天');
        $('select[name="paymentMethod"]').attr('disabled','').val('BulletRepayment');
        $day.attr('name','dueDay');
    };
    if ($("input[name='dueUnit']:checked").attr('value') == "月") {
        $('span.changeUnit').html('月');
        $('select[name="paymentMethod"]').removeAttr('disabled');
        $day.attr('name','dueMonth');
    };

});


/**
 * 跳转页面切换
 *
*/

// $(".nav .nav-mid li a").each(function(){

//     if ($(this)[0].href == String(window.location)) {

//         $(this).parent().addClass('active').children().attr('href','javascript:;');

//     };

// });


/**
 * 分页居中
 *
*/

    //var ulpaWidth = $('ul.pagination').width();
    //var ulpaLeft = - ulpaWidth / 2 ;
    //$('ul.pagination').css('right','50%').css('margin-right',ulpaLeft);

/**
 *  页面跳转导航高亮
 *
*/
    String.prototype.getQuery = function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); 
        return null;
    }

    // var aUrl = window.location.href;
    //var aUrl = "http://ypt.yangpiaodai.com/about_contact_us?mode=3&nav=0";
    var $nav = $('.nav-mid ul li a');
    var $navTop = $('.nav-top ul li a');

    //console.log(aUrl + '||' + nav.length + "||" + aUrl.getQuery("mode") + "||" + aUrl.getQuery("nav"));
    //if (aUrl.getQuery("mode") == "3") {
    //    // nav.parents('ul').find('li').eq(3).addClass('active');
    //};
    //
    //for (var i = 0; i < nav.length; i++) {
    //    var url = nav.eq(i).prop('href');
    //    if (aUrl.indexOf(url) != -1) { /*如果链接的href值在当前页面地址中有匹配*/
    //        nav.eq(i).parent().addClass('active');
    //    }
    //};
    //
    //for (var i = 0; i < navTop.length; i++) {
    //    var url = navTop.eq(i).prop('href');
    //    if (aUrl.indexOf(url) != -1) { /*如果链接的href值在当前页面地址中有匹配*/
    //        navTop.eq(i).parent().addClass('active');
    //    }
    //};
    $nav.each(function(){

        if ($(this)[0].href == String(window.location)) {
            $(this).parent().addClass('active').children().attr('href','javascript:void(0);');
        };

    });

    $navTop.each(function(){

        if ($(this)[0].href == String(window.location)) {
            $(this).parent().addClass('active').children().attr('href','javascript:void(0);');
        };

    });

    $(".about-ul li a").each(function(){

        if ($(this)[0].href == String(window.location)) {
            $(this).parent().addClass('active').children().attr('href','javascript:void(0);');
        };

    });

    $(".user-dl dd a").each(function () {

        if ($(this)[0].href == String(window.location)){
            $(this).parent().addClass('active').children().attr('href', 'javascript:void(0);');
        }  

    });

/*
 * 顶部导航内容显示隐藏
 *
*/

$('.head .top .nav-top li').click(function(){   

    if ($(this).children('i').attr('rel') == 'hide') {
        $(this).children('i').attr('rel','show').show();
        setTimeout("$('.head .top .nav-top li i').hide().attr('rel','hide');",1000);
    }else{
        $(this).children('i').attr('rel','hide').hide();
    };

});
/**
 * 手机自适应屏幕
*/
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
    var viewportmeta = document.querySelector('meta[name="viewport"]');
    if (viewportmeta) {
     viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
     document.addEventListener('touchstart', function () {
     viewportmeta.content = 'viewportmeta.content = width=device-width, minimum-scale=0.25, maximum-scale=1.6';
    }, false);
    document.addEventListener('orientationchange', function () {
     viewportmeta.content = 'viewportmeta.content = width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
    }, false);
    }
}